
/*
CEmitter.cpp
CEmitter element

*/



#include "Emitter.h"


CEmitter::CEmitter(void)
{
	
	counter=0;

	m_nPosX	=0;
	m_nPosY	=0;
	
	m_fSpawnRate=1;
	m_fSpawncount=0;
	
	m_fScaleX_Start	=0;
	m_fScaleX_End	=0;
	m_fScaleY_Start	=0;
	m_fScaleY_End	=0;
	
	m_fColorR_Start	=1;
	m_fColorR_End	=1;
	m_fColorG_Start	=1;
	m_fColorG_End	=1;
	m_fColorB_Start	=1;
	m_fColorB_End	=1;
	m_fColorA_Start	=1;
	m_fColorA_End	=1;
	
	m_fRotation_Start	=1;
	m_fRotation_End	=1;
	
	m_strName ="Nope";
	
	m_bLooping	=false;
	
//	m_nImageID =-1;
	
	m_nLife_Max	=0;
	m_nLife_Min	=0;
	
	m_nMaxParticles	=0;
	
	m_nSourceBlend	=0;
	m_nDestBlend	=0;

		m_rRECT.bottom=500;
		m_rRECT.left=500;
		m_rRECT.right=400;
		m_rRECT.top=400;

}


CEmitter::~CEmitter(void)
{
	for(m_lDeadIter =m_lDead.begin();m_lDeadIter!=m_lDead.end();m_lDeadIter++)
	{
		delete((*m_lDeadIter));
	}
}
void CEmitter::Load(void)
{
		
	m_nPosX	=100;
	m_nPosY	=100;
	
	m_fSpawnRate	=1;
	m_fSpawncount = 0;
	m_fScaleX_Start	=1;
	m_fScaleX_End	=1;
	m_fScaleY_Start	=1;
	m_fScaleY_End	=1;
	
	m_fColorR_Start	=100;
	m_fColorR_End	=255;
	m_fColorG_Start	=100;
	m_fColorG_End	=255;
	m_fColorB_Start	=100;
	m_fColorB_End	=255;
	m_fColorA_Start	=10;
	m_fColorA_End	=255;
	
	m_fRotation_Start	=0;
	m_fRotation_End	=0;
	
	m_strName ="Nope";
	
	m_bLooping	=true;
	
	
	
	m_nLife_Max	=50;
	m_nLife_Min	=10;
	
	m_nMaxParticles	=5;
	
	m_fVelXS =10;
	m_fVelXE =50;
	m_fVelYS =10;
	m_fVelYE =50;

	m_nSourceBlend	=1;
	m_nDestBlend	=1;

		m_rRECT.bottom=200;
		m_rRECT.left=100;
		m_rRECT.right=200;
		m_rRECT.top=100;


		for(int x=0; x<m_nMaxParticles;x++)
		{
			m_lDead.push_back(Generate());
		}
			//generate to deadlist
}


void CEmitter::Update( float fElapsedTime )
{
	CEntity::Update(fElapsedTime);

	if(m_bLooping==true)
	{
	if(m_lLive.size()<m_nMaxParticles)
	{
		m_fSpawncount+=fElapsedTime;
		if (m_fSpawncount>=m_fSpawnRate)
		{
		//m_lLive.push_back(Generate());//generate and add to live list
			m_lDeadIter	=m_lDead.begin();
			Reset(*m_lDeadIter);
			m_lLive.push_back(*m_lDeadIter);
			m_lDead.pop_front();
		m_fSpawncount=0;
		}
	}
	}
	else
	{
		if(counter<m_nMaxParticles)
	{
		m_fSpawncount+=fElapsedTime;
		if (m_fSpawncount>=m_fSpawnRate)
		{
		//m_lLive.push_back(Generate());//generate and add to live list
			m_lDeadIter	=m_lDead.begin();
			Reset(*m_lDeadIter);
			m_lLive.push_back(*m_lDeadIter);
			m_lDead.pop_front();
		m_fSpawncount=0;
		counter++;
		}
	}
	}


	for(m_lLiveIter =m_lLive.begin();m_lLiveIter!=m_lLive.end();m_lLiveIter++)
		{
			if(m_bLooping==true)
			{
			if((*m_lLiveIter)->IsAlive()==false)
			{
				
				Reset(*m_lLiveIter); //reset;
			
			}
				(*m_lLiveIter)->Update(fElapsedTime);
			}
			else
				{
				if((*m_lLiveIter)->IsAlive()==false)
					{
					m_lDead.push_back(*m_lLiveIter);
					m_lLive.remove((*m_lLiveIter));
					break;
					}
				else
					(*m_lLiveIter)->Update(fElapsedTime);
				}
	}
	//for(m_lLiveIter =m_lLive.begin();m_lLiveIter!=m_lLive.end();m_lLiveIter++)
	//{
	//	for(m_lDeadIter =m_lDead.begin();m_lDeadIter!=m_lDead.end();m_lDeadIter++)
	//	if((*m_lLiveIter)==(*m_lDeadIter))
	//	{
	//	m_lLive.remove((*m_lLiveIter));
	//	break;
	//	}
	//		
	//}

}
void CEmitter::Render( void )
{
		CSGD_Direct3D*		pD = CSGD_Direct3D::GetInstance();
		DWORD source,dest;
		pD->GetDirect3DDevice()->GetRenderState(D3DRS_SRCBLEND,&source);
		pD->GetDirect3DDevice()->GetRenderState(D3DRS_DESTBLEND,&dest);
		pD->GetDirect3DDevice()->SetRenderState(D3DRS_SRCBLEND,GetSBlend());
		pD->GetDirect3DDevice()->SetRenderState(D3DRS_DESTBLEND,GetDBlend());
	
	for(m_lLiveIter =m_lLive.begin();m_lLiveIter!=m_lLive.end();m_lLiveIter++)
	{
			(*m_lLiveIter)->Render();
	}
	
			pD->GetDirect3DDevice()->SetRenderState(D3DRS_SRCBLEND,source);
		pD->GetDirect3DDevice()->SetRenderState(D3DRS_DESTBLEND,dest);
}

	CParticle*  CEmitter::Generate(void)
	{
		CSGD_TextureManager* m_pTM	=  CSGD_TextureManager::GetInstance();
	// int X		=m_pTM->LoadTexture(_T("resource/graphics/MLB_SGD_Tank.png"),	D3DCOLOR_XRGB(0,0,0) );
	

		CParticle* temp = new CParticle();
		temp->SetLife(rand()%(m_nLife_Max-m_nLife_Min));
		temp->SetColorR(m_fColorR_Start,m_fColorR_End);
		temp->SetColorG(m_fColorG_Start,m_fColorG_End);
		temp->SetColorB(m_fColorB_Start,m_fColorB_End);
		temp->SetColorA(m_fColorA_Start,m_fColorA_End+1);
		temp->SetCOTR();
		temp->SetCOTG();
		temp->SetCOTB();
		temp->SetCOTA();
		temp->SetRotation(m_fRotation_Start,m_fRotation_End);
		temp->SetScale(m_fScaleX_Start,m_fScaleX_End,m_fScaleY_Start,m_fScaleY_End); // need to make this xs,xe,ys,ye setting fo scalling Call setSOT?
		temp->SetHeight(m_nImageX);//testfiles need to add variable... 
		temp->SetWidth(m_nImageY);//testfiles need to add variable
		temp->SetOrigin((rand()%(m_rRECT.right-m_rRECT.left)+m_nPosX),(rand()%(m_rRECT.bottom-m_rRECT.top)+m_nPosY));
		
		temp->SetVelX(((rand()%((int)m_fVelXS-(int)m_fVelXS2))+(int)m_fVelXS2),((rand()%((int)m_fVelXE-(int)m_fVelXE2))+(int)m_fVelXE2));																
		temp->SetVelY(((rand()%((int)m_fVelYS-(int)m_fVelYS2))+(int)m_fVelYS2),((rand()%((int)m_fVelYE-(int)m_fVelYE2))+(int)m_fVelYE2));
		temp->SetImageID(m_nImageIDs);	//need to pull value directly from xml.
		return temp;
	}

	void CEmitter::Reset(CParticle* bob)
	{
		bob->SetLife(rand()%(m_nLife_Max-m_nLife_Min)+1);
		bob->SetColorR(m_fColorR_Start,m_fColorR_End);
		bob->SetColorG(m_fColorG_Start,m_fColorG_End);
		bob->SetColorB(m_fColorB_Start,m_fColorB_End);
		bob->SetColorA(m_fColorA_Start,m_fColorA_End+1);
		bob->SetCOTR();
		bob->SetCOTG();
		bob->SetCOTB();
		bob->SetCOTA();
		bob->SetRotation(m_fRotation_Start,m_fRotation_End);
		bob->SetScale(m_fScaleX_Start,m_fScaleX_End,m_fScaleY_Start,m_fScaleY_End); // need to make this xs,xe,ys,ye setting fo scalling Call setSOT?
		bob->SetHeight(m_nImageX);//testfiles need to add variable... 
		bob->SetWidth(m_nImageY);//testfiles need to add variable
		bob->SetOrigin((rand()%(m_rRECT.right-m_rRECT.left)+m_nPosX),(rand()%(m_rRECT.bottom-m_rRECT.top)+m_nPosY));
		bob->SetVelX(((rand()%((int)m_fVelXS-(int)m_fVelXS2))+(int)m_fVelXS2),((rand()%((int)m_fVelXE-(int)m_fVelXE2))+(int)m_fVelXE2));																
		bob->SetVelY(((rand()%((int)m_fVelYS-(int)m_fVelYS2))+(int)m_fVelYS2),((rand()%((int)m_fVelYE-(int)m_fVelYE2))+(int)m_fVelYE2));
		bob->SetImageID(m_nImageIDs);	

	}




	 CEmitter* CEmitter::LoadParticle(const char* szFileName) //,int IID)
	{
			m_pTM	=  CSGD_TextureManager::GetInstance();
		TiXmlDocument doc;
	
		if(doc.LoadFile(szFileName)==false)
			return nullptr;
		
		TiXmlElement* pRoot = doc.RootElement();
		if(pRoot==nullptr)
			return nullptr;
	
	
		TiXmlElement* pParticle = pRoot->FirstChildElement("particle_info");
		int x =-1;
		int B,T,L,R;
		while(pParticle!=nullptr)
		{
		
			TCHAR  	bufferA[ 1000 ];
			const char* pText = pParticle->GetText();
			
			//char* texts ="Resource//";
			if(pText==nullptr)
				m_strName=pText;
		//	strcat(texts,pText);
			mbstowcs_s(nullptr,bufferA, 1000,  pText, _TRUNCATE );
			
			m_nImageIDs=m_pTM->LoadTexture(bufferA,D3DCOLOR_XRGB(255,255,255));
			pParticle->Attribute("PosX",&m_nPosX);
			pParticle->Attribute("PosY",&m_nPosY);
			pParticle->QueryFloatAttribute("SpawnRate",&m_fSpawnRate);
			m_fSpawncount=0;
			pParticle->QueryFloatAttribute("ScaleX_Start",&m_fScaleX_Start);
			pParticle->QueryFloatAttribute("ScaleX_End",&m_fScaleX_End);
			pParticle->QueryFloatAttribute("ScaleY_Start",&m_fScaleY_Start);
			pParticle->QueryFloatAttribute("ScaleY_End",&m_fScaleY_End);
			pParticle->QueryFloatAttribute("VELXSTART",&m_fVelXS);
			pParticle->QueryFloatAttribute("VELXSTART2",&m_fVelXS2);
			pParticle->QueryFloatAttribute("VELXEND",&m_fVelXE);
			pParticle->QueryFloatAttribute("VELXEND2",&m_fVelXE2);
			pParticle->QueryFloatAttribute("VELYSTART",&m_fVelYS);
			pParticle->QueryFloatAttribute("VELYEND",&m_fVelYE);
			pParticle->QueryFloatAttribute("VELYSTART2",&m_fVelYS2);
			pParticle->QueryFloatAttribute("VELYEND2",&m_fVelYE2);


			pParticle->QueryFloatAttribute("RStart",&m_fColorR_Start	);
			pParticle->QueryFloatAttribute("REnd",  &m_fColorR_End	);
			pParticle->QueryFloatAttribute("GStart",&m_fColorG_Start	);
			pParticle->QueryFloatAttribute("GEnd",  &m_fColorG_End	);
			pParticle->QueryFloatAttribute("BStart",&m_fColorB_Start	);
			pParticle->QueryFloatAttribute("BEnd",  &m_fColorB_End	);
			pParticle->QueryFloatAttribute("AStart",&m_fColorA_Start	);	
			pParticle->QueryFloatAttribute("AEnd",  &m_fColorA_End	);	
			pParticle->QueryFloatAttribute("RotationStart",&m_fRotation_Start);	
			pParticle->QueryFloatAttribute("RotationEnd",&m_fRotation_End);
			pParticle->Attribute("Life_Max",&m_nLife_Max);
			pParticle->Attribute("Life_Min",&m_nLife_Min);
			pParticle->Attribute("Bottom",&B);	m_rRECT.bottom=B; 
			pParticle->Attribute("Left",&L);	m_rRECT.left=L;
			pParticle->Attribute("Right",&R);	m_rRECT.right=R;
			pParticle->Attribute("Top",&T);	m_rRECT.top=T;
			pParticle->Attribute("SourceBlend",&m_nSourceBlend	);
			pParticle->Attribute("DestBlend",&m_nDestBlend	);
			pParticle->Attribute("MaxParticles",&m_nMaxParticles	);
			pParticle->Attribute("IX",&m_nImageX	);
			pParticle->Attribute("IY",&m_nImageY	);

			
			pParticle->Attribute("Looping",&x);
			if(x==12)
			m_bLooping	=true;//true;
			else
				m_bLooping	=false;
			//m_nImageIDs ;				
				
			//m_nImageIDs=IID;
			
			pParticle=pParticle->NextSiblingElement("particle_info");
	
	
		}
	
	 
		for(int x=0; x<m_nMaxParticles;x++)
		{
			m_lDead.push_back(Generate());
		}

			return this;
		
	 }